package com.yf.construction.storage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.construction.storage.convert.StorageConvert;
import com.yf.construction.storage.dto.ConstructionStorageDTO;
import com.yf.construction.storage.entity.ConstructionStorage;
import com.yf.construction.storage.mapper.ConstructionStorageMapper;
import com.yf.construction.storage.service.ConstructionStorageService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 业务实现类
 * </p>
 *
 * @author anyone
 * @since 2024-09-13 16:38
 */
@Service
public class ConstructionStorageServiceImpl extends ServiceImpl<ConstructionStorageMapper, ConstructionStorage> implements ConstructionStorageService {

    @Override
    public Page<ConstructionStorageDTO> paging(PagingReqDTO<ConstructionStorageDTO> reqDTO) {
        //查询条件
        QueryWrapper<ConstructionStorage> wrapper = new QueryWrapper<>();
        // 请求参数
        ConstructionStorageDTO params = reqDTO.getParams();
        wrapper.eq(StrUtil.isNotBlank(params.getDataFormat()), "data_format", params.getDataFormat());
        wrapper.eq(StrUtil.isNotBlank(params.getStorageLocation()), "storage_location", params.getStorageLocation());
        //获得数据
        Page<ConstructionStorage> page = this.page(reqDTO.toPage(), wrapper);
        //转换结果
        return StorageConvert.INSTANCE.pageEntityDto(page);
    }


    @Override
    public void save(ConstructionStorageDTO reqDTO) {
        //复制参数
        ConstructionStorage entity = StorageConvert.INSTANCE.dtoToEntity(reqDTO);
        this.saveOrUpdate(entity);
    }

    @Override
    public void delete(List<String> ids) {
        //批量删除
        this.removeByIds(ids);
    }

    @Override
    public ConstructionStorageDTO detail(String id) {
        ConstructionStorage entity = this.getById(id);
        return StorageConvert.INSTANCE.entityToDto(entity);
    }

    @Override
    public List<ConstructionStorageDTO> list(ConstructionStorageDTO reqDTO) {
        //分页查询并转换
        QueryWrapper<ConstructionStorage> wrapper = new QueryWrapper<>();
        //转换并返回
        List<ConstructionStorage> list = this.list(wrapper);
        //转换数据
        return StorageConvert.INSTANCE.listEntityDto(list);
    }
}
